﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace SharpSoft.Net.WebSockets
{
    using System.Net.WebSockets;
    using SharpSoft.Net.WebSockets;
    using System.Threading.Tasks;

    public static class WebSocketExtension
    {
        public static async Task SendMessageAsync(this WebSocket socket, WebSocketMessage msg)
        {
            if (socket.State == WebSocketState.Open)
            {
                var txt = System.Text.Json.JsonSerializer.Serialize(msg);
                var data = System.Text.Encoding.UTF8.GetBytes(txt);
                ArraySegment<byte> segment = new ArraySegment<byte>(data);

                await socket.SendAsync(segment, WebSocketMessageType.Text, true, new System.Threading.CancellationToken());
            }

        }
    }
}
